package com.rs.admin.controller.sy;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rs.admin.common.RespResult;
import com.rs.admin.mapper.DictInfoMapper;
import com.rs.admin.module.DictInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "字典操作")
@RestController
@RequestMapping("/dict")
public class DictInfoController {

    @Resource
    DictInfoMapper dictInfoMapper;


//    @ApiOperation("校验字典名称和key是否存在")
    @PostMapping("/checkNameKey")
    public RespResult checkNameKey(@RequestBody JSONObject params) {

        String dictLabel = params.getString("dictLabel");
        String dictKey = params.getString("dictKey");

        LambdaQueryWrapper<DictInfo> dictLabelQuery = Wrappers.lambdaQuery();
        dictLabelQuery.eq(DictInfo::getDictLabel, dictLabel);
        DictInfo labelInfo = dictInfoMapper.selectOne(dictLabelQuery);
        if (null != labelInfo) {
            return RespResult.buildFail("标签名称已经存在");
        }

        LambdaQueryWrapper<DictInfo> dictKeyQuery = Wrappers.lambdaQuery();

        dictKeyQuery.eq(DictInfo::getDictKey, dictKey);
        DictInfo keyInfo = dictInfoMapper.selectOne(dictKeyQuery);

        if (null != keyInfo) {
            return RespResult.buildFail("标签名称已经存在");
        }

        return RespResult.buildSuccess();

    }

    @ApiOperation("添加字典")
    @PostMapping("add")
    public RespResult add(@RequestBody DictInfo dict) {

        if (null != dict.getId()) {
            dictInfoMapper.updateById(dict);
        } else {
            dictInfoMapper.insert(dict);
        }

        return RespResult.buildSuccess();

    }


    @ApiOperation("删除")
    @PostMapping("delete")
    public RespResult delete(@RequestBody DictInfo dict) {

        dictInfoMapper.deleteById(dict.getId());
        return RespResult.buildSuccess();

    }


    @ApiOperation("根据key 获取字典 ")
    @GetMapping("getByKey")
    public RespResult getByKey(@RequestParam String dictKey) {

        LambdaQueryWrapper<DictInfo> dictKeyQuery = Wrappers.lambdaQuery();

        dictKeyQuery.eq(DictInfo::getDictKey, dictKey);

        DictInfo keyInfo = dictInfoMapper.selectOne(dictKeyQuery);

        return RespResult.buildSuccess(keyInfo);

    }



    @ApiOperation("列表接口")
    @GetMapping("list")
    public RespResult list(String dictLabel,Integer page,Integer pageSize) {

        page = null == page?1:page;

        pageSize = null == pageSize?10:pageSize;

        LambdaQueryWrapper<DictInfo> dictKeyQuery = Wrappers.lambdaQuery();
        if(!StringUtils.isEmpty(dictLabel)){
            dictKeyQuery.like(DictInfo::getDictLabel,dictLabel);
        }

        PageHelper.startPage(page,pageSize);

        List<DictInfo> list = dictInfoMapper.selectList(dictKeyQuery);
        PageInfo<DictInfo> pageInfo = new PageInfo<>(list);

        Map<String,Object> data = new HashMap<>();
        data.put("rows",list);
        data.put("page",page);
        data.put("pageSize",pageSize);
        data.put("total",pageInfo.getTotal());

        return RespResult.buildSuccess(data);


    }



}
